package code.structure.module.scene.view.iso.product.base
{
	import code.main.AppData;
	import code.structure.module.configs.data.ItemConfigData;
	import code.structure.module.scene.model.data.SceneItemData;
	import code.structure.module.scene.view.YIsoWorld;
	import code.structure.module.scene.view.iso.YIsoObject;
	import code.tools.other.SecondDrive;
	import code.untils.GameConst;
	import code.untils.IsoStatusConst;
	import code.untils.ItemTypeConst;
	import code.untils.ProductStatusConst;
	import code.untils.SceneConfig;
	
	import flash.events.Event;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	
	/**
	 * 生产型 isoobject基类 
	 * @author Yuri
	 * 
	 */
	public class ProductIsoObject extends YIsoObject
	{
		
		
		private var _timerCount:int;
		protected var _ripeCycleCount:int;
		
		private var _productStatus:String = ProductStatusConst.PRODUCT_INIT_STATUS;
		
		protected var _productShopItemData:ItemConfigData;
		
		protected var _isHaveWithered:Boolean = false;
		
		/**
		 *生产过程中回调函数 
		 */
		private var _productProgressCallBackFunList:Array = [];
		
		/**
		 *是否可以展示生产动画 
		 */
		protected var _isStartProductShow:Boolean;
		/**
		 * 生产动画数组 
		 */
		protected var _productAnimationList:Array = [];
		/**
		 * 成熟动画数组 
		 */
		protected var _ripeAnimationList:Array = [];
		/**
		 * 枯萎、过期动画数组 
		 */
		protected var _witheredAnimationList:Array = [];
		
		
		private var _doActionProductStatus:String;
		
		/**
		 *枯萎时间 
		 */
		protected var WITHERED_TIME:int = GameConst.WITHERED_TIME_PLANT;
		
		public function ProductIsoObject()
		{
			super();
			mouseTipEnable = true;
			selfTipEnable = true;
		}
		
		override protected function refresh():void{
			super.refresh();
			if(id) refreshProductData();
		}
		
		/**
		 * 
		 * 刷新生产信息 进行后续操作
		 */
		protected function refreshProductData():void
		{
			
		}	
		
		/**
		 *主体加载完成后函数 
		 * 
		 */
		override protected function mainBodyLoadCompleteNext():void{
			if(_isBodyAnimation)
				createAnimation(_bitmapList,animationFrame);
			refreshProductMaterial();
		}
		
		protected function refreshProductMaterial():void{
			if(isMaterialLoadComplete){
				if(!_isBodyAnimation)
				switch(productStatus){
					case ProductStatusConst.PRODUCT_LOCKED_STATUS:
						jumpLockedStatus();
						break;
					case ProductStatusConst.PRODUCT_INIT_STATUS:
						jumpInitStatus();
						break;
					case ProductStatusConst.PRODUCT_PREPARE_PRODUCT_STATUS:
						_isStartProductShow = false;
						break;
					case ProductStatusConst.PRODUCT_PROGRESS_STATUS:
						//要根据时间进行判断
						jumpProgressStatus();
						break;
					case ProductStatusConst.PRODUCT_MATURE_STATUS:
						jumpMatureStatus();
						break;
					case ProductStatusConst.PRODUCT_WITHERED_STATUS:
						jumpWitheredStatus();
						break;
				}
				if(!selfTipEnable) return;
				if(productStatus == ProductStatusConst.PRODUCT_LOCKED_STATUS){
					createTip();
//				}else if(productStatus == ProductStatusConst.PRODUCT_INIT_STATUS && (_data.data.subType == ItemTypeConst.MARKET_PET )){
//					createTip();
				}else if(productStatus == ProductStatusConst.PRODUCT_MATURE_STATUS){
					createTip();
				}else if(productStatus == ProductStatusConst.PRODUCT_PROGRESS_STATUS && isCanUpgrade){
					createTip();
				}else{
					removeTip(true);
				}
			}
		}
		
		protected function jumpInitStatus():void{
			jumpToFrame(1);
		}
		
		protected function jumpLockedStatus():void{
			
		}
		
		protected function jumpProgressStatus():void{
			
		}
		
		protected function jumpMatureStatus():void{
			
		}
		
		protected function jumpWitheredStatus():void{
			
		}
		
		/**
		 *生产 
		 * 
		 */
		protected function product(isNeedChangeStatus:Boolean = true):void{
			_timerCount = 0;
			ripeCycleCount = data.endTime;
			if(isNeedChangeStatus)productStatus = ProductStatusConst.PRODUCT_PROGRESS_STATUS;
			createOutPutData();
			checkIsNeedClock();
			
		}
		
		/**
		 *生产过程 
		 * 
		 */
		protected function onProductProgressHandler():void
		{
			_timerCount++;
			data.endTime = ripeCycleCount-_timerCount;
			if(timerCount == ripeCycleCount){
				data.endTime = 0;//倒计时完成
				countDownRipe();
				ripe();
				productProgressFun(0);
			}else{
				productProgressFun(ripeCycleCount-_timerCount);
			}
		}
		
		/**
		 * 倒计时成熟调用
		 */
		protected function countDownRipe():void {
			
		}
		/**
		 *生产过程回调 
		 * @param leftTime
		 * 
		 */
		protected function productProgressFun(leftTime:int):void{
			for each(var fun:Function in _productProgressCallBackFunList){
				fun(leftTime);
			}
		}
		
		/**
		 *增加生产过程回调函数 
		 * @param fun
		 * 
		 */
		public function addProductProgressCallBackFun(fun:Function,isNeedCallNow:Boolean=true):void{
			if(_productProgressCallBackFunList.indexOf(fun)==-1){
				if(isNeedCallNow) fun(ripeCycleCount-_timerCount);
				_productProgressCallBackFunList.push(fun);
			}
		}
		
		/**
		 *移除生产过程回调函数 
		 * @param fun
		 * 
		 */
		public function removeProductProgressCallBackFun(fun:Function):void{
			var index:int = _productProgressCallBackFunList.indexOf(fun);
			if(index!=-1)
				_productProgressCallBackFunList.splice(index,1);
		}
		
		/**
		 *成熟 
		 * 
		 */
		protected function ripe():void{
			SecondDrive.removeListener(onProductProgressHandler);
//			data.endTime = 0;
			_timerCount = 0;
			ripeCycleCount = 0;
			createOutPutData();
			productStatus = ProductStatusConst.PRODUCT_MATURE_STATUS;
			checkIsNeedClock();
		}
		
		/**
		 * 生成 OutPutData
		 * 
		 */
		protected  function createOutPutData():void{
			if(data.outputData) return;
//			if(productShopItemData && data.data.subType!=ItemTypeConst.MARKET_KITCHEN_WARE && !data.outputData){ //这里判断非烹饪设施的赋值
//				data.output_id = productShopItemData.productAttachData.outputId;
//				data.output_num = productShopItemData.productAttachData.outputNum;
//				data.outputData = productShopItemData.productAttachData.outputData;
//			}
		}
		
		/**
		 *枯萎
		 * 
		 */
		protected function withered():void{
			productStatus = ProductStatusConst.PRODUCT_WITHERED_STATUS;
		}
		
		/**
		 *检查是否枯萎 只对有枯萎情况下调用
		 * 
		 */
		protected function checkIfWithered():Boolean{
			if(data.endTime<=-WITHERED_TIME){
				withered();
				return true;
			}else{
				ripe();
			}
			return false;
		}
		
		/**
		 *枯萎进度 
		 * 
		 */
		private function onWitheredProgressHandler():void
		{
			// TODO Auto Generated method stub
			data.endTime--; 
			if(data.endTime<=-WITHERED_TIME){
				withered();
				SecondDrive.removeListener(onWitheredProgressHandler);
			}
		}
		
		/**
		 *检测是否需要计时器 
		 * 
		 */
		public function checkIsNeedClock():void{
			if(!isUnlock || !AppData.isMyScene) return;//未解锁的或者不是自己场景的不需要
			switch(productStatus){
				case  ProductStatusConst.PRODUCT_MATURE_STATUS:
					if(isHaveWithered && !SecondDrive.hasEventListener(onWitheredProgressHandler))
						SecondDrive.addEventListener(onWitheredProgressHandler, 1000);
					break;
				case ProductStatusConst.PRODUCT_PROGRESS_STATUS:
				case ProductStatusConst.PRODUCT_PREPARE_PRODUCT_STATUS:
					if(!SecondDrive.hasEventListener(onProductProgressHandler))
						SecondDrive.addEventListener(onProductProgressHandler, 1000);
					break;
			}
		}
		
		/**
		 *是否取消进入枯萎倒计时 
		 * @param value
		 * 
		 */
		public function set cancleEnterWitheredClock(value:Boolean):void{
			if(productStatus == ProductStatusConst.PRODUCT_MATURE_STATUS){
				if(value && SecondDrive.hasEventListener(onWitheredProgressHandler)){
					SecondDrive.removeListener(onWitheredProgressHandler);
				}else if(!value){
					checkIsNeedClock();
				}
			}
		}
		
		/**
		 *造好回调 
		 * 
		 */
		override public function initBuildCallFun():void{
			if(data) id = data.id;
		}
		
		/**
		 *收获回调 
		 * 
		 */
		public function harvestCallFun():void{
			if(data){
				data.endTime = 0;
			}
			cancelWitheredProgress();
		}
		
		/**
		 *取消枯萎过程 
		 * 
		 */
		protected function cancelWitheredProgress():void{
			if(isHaveWithered){
				SecondDrive.removeListener(onWitheredProgressHandler);
			}
		}
		
		
		/**
		 *取消生产过程 
		 * 
		 */
		protected function cancelProductProgress():void{
			SecondDrive.removeListener(onProductProgressHandler);
		}
		
		/**
		 *枯萎清除回调 
		 * 
		 */
		public function witheredCallFun():void{
			if(parentWorld){
				parentWorld.removeFromWorld(this);
			}
		}
		
		
		override public function set data(value:SceneItemData):void
		{
			_data = value;
			if(value){
				id = value.id;
				productShopItemData = data.data;
				refresh();
			}
		}
		
		public function get productStatus():String
		{
			return _productStatus;
		}
		
		public function set productStatus(value:String):void
		{
			_productStatus = value;
			refreshProductMaterial();
		}
		
		public function get timerCount():int
		{
			return _timerCount;
		}
		
		public function set timerCount(value:int):void
		{
			_timerCount = value;
		}
		
		/**
		 *成熟时间 
		 */
		public function get ripeCycleCount():int
		{
			return _ripeCycleCount;
		}
		
		/**
		 * @private
		 */
		public function set ripeCycleCount(value:int):void
		{
			_ripeCycleCount = value;
		}
		
		/**
		 *产物宿主配置数据 
		 */
		public function get productShopItemData():ItemConfigData
		{
			return _productShopItemData;
		}
		
		/**
		 * @private
		 */
		public function set productShopItemData(value:ItemConfigData):void
		{
			_productShopItemData = value;
		}
		
		/**
		 *是否有枯萎状态 
		 * @return 
		 * 
		 */
		public function get isHaveWithered():Boolean
		{
			return _isHaveWithered;
		}
		
		public function set isHaveWithered(value:Boolean):void
		{
			if(AppData.isNew){
				_isHaveWithered = false;
				return;
			}
			_isHaveWithered = value;
		}
		
		
		/**
		 * 减少生产时间 
		 * @param reduceTime
		 * 
		 */
		public function reduceProductTime(reduceTime:int,isNeedAddOutNum:Boolean=false):void{
			if(productStatus==ProductStatusConst.PRODUCT_PROGRESS_STATUS){
				ripeCycleCount -= reduceTime;
				if(timerCount >= ripeCycleCount){
					data.endTime = 0;//倒计时完成
					ripe();
					productProgressFun(0);
					if(isNeedAddOutNum) data.output_num +=1;
				}
			}
		}
		
		/**
		 *立即成熟 
		 * 
		 */
		public function ripeNow(isCheckStatus:Boolean = true):void{
			if(isCheckStatus && productStatus!=ProductStatusConst.PRODUCT_PROGRESS_STATUS) return;
			data.endTime = 0;
			ripe();
			productProgressFun(0);
		}
		
		/**
		 *  挽救枯萎
		 * @param saveWithered
		 * 
		 */
		public function saveWithered():void{
			if(!isHaveWithered) return;
			if(productStatus==ProductStatusConst.PRODUCT_WITHERED_STATUS){
				data.endTime = 0;
				ripe();
			}
		}
		
		/**
		 *取消生产 
		 * 
		 */
		public function cancelProduct():void{
			if(productStatus==ProductStatusConst.PRODUCT_PROGRESS_STATUS){
				cancelProductProgress();
			}
		}

		/**
		 * 加载完成生产动画机器转换位图 
		 * @param subKey
		 * @param bitmapArr
		 * 
		 */
		protected function onProductAnamationCoverCompleteFun(bitmapArr:Array):void
		{
			_productAnimationList = bitmapArr;
			if(productStatus !=ProductStatusConst.PRODUCT_PROGRESS_STATUS) return;
			
			_isNeedDelayIntervelAnimation =false;
			createAnimation(_productAnimationList,15);
			if(!_isStartProductShow){
				_animationBitmap.moviePlayer.goToAndStop(1);
			}
			_isChangeFacade = true;
		}
		
		/**
		 * 加载完成成熟动画转换位图 
		 * @param subKey
		 * @param bitmapArr
		 * 
		 */
		protected function onRipeAnamationCoverCompleteFun(bitmapArr:Array):void
		{
			_ripeAnimationList = bitmapArr;
			if(productStatus !=ProductStatusConst.PRODUCT_MATURE_STATUS) return;
			_isNeedDelayIntervelAnimation = true;
			createAnimation(_ripeAnimationList,15);
			_isChangeFacade = true;
		}
		
		/**
		 * 加载完成过期枯萎动画转换位图 
		 * @param subKey
		 * @param bitmapArr
		 * 
		 */
		protected function onWitheredAnamationCoverCompleteFun(bitmapArr:Array):void
		{
			_witheredAnimationList = bitmapArr;
			if(productStatus !=ProductStatusConst.PRODUCT_WITHERED_STATUS) return;
			
			_isNeedDelayIntervelAnimation = false;
			createAnimation(_witheredAnimationList,15);
			_isChangeFacade = true;
		}
		
		private var tOut:int;

		override protected function onMotionCircleOverHandler(event:Event):void
		{
			clearTimeout(tOut);
			pauseAnimation(1);
			var s:int = Math.ceil(Math.random()*30)*1000;
			tOut = setTimeout(function ():void{playAnimation()},s);
		}
		
		override protected function removeMotionListener():void{
			clearTimeout(tOut);
			super.removeMotionListener();
		}
		
		override public function get verticalHeight():Number{
			return super.verticalHeight;
		}
		
		override public function set isUnlock(value:Boolean):void{
			super.isUnlock = value;
			if(isUnlock) checkIsNeedClock();
		}
		
		/**
		 *更新鼠标响应属性 
		 * 
		 */
		override public function refreshEnable():void{
			if(!AppData.isMyScene){
//				mouseMoveEnabled = clickEnabled = isNeedHelp;
				isResponseMouse = false;
				return;
			}else{
				if(parentWorld){
					isResponseMouse = (parentWorld as YIsoWorld).status == SceneConfig.SCENE_DIY?isCanDiy:true;
				}
			}
		}
		
		override public function destroy():void{
			removeMotionListener();
			super.destroy();
			_productProgressCallBackFunList = [];
			SecondDrive.removeListener(onWitheredProgressHandler);
			SecondDrive.removeListener(onProductProgressHandler);
		}
		
		/**
		 * 清除产出生信息
		 * 
		 */
		protected function clearOutPutData():void{
			if(!data) return;
			data.output_id = 0;
			data.output_num = 0;
			data.outputData = null;
		}

		/**
		 *执行时所记录的生产状态 
		 */
		public function get doActionProductStatus():String
		{
			return _doActionProductStatus;
		}

		/**
		 * @private
		 */
		public function set doActionProductStatus(value:String):void
		{
			_doActionProductStatus = value;
		}
	}
}